import * as requsets from './api.js'
module.exports = (vm) => {
    // 初始化请求配置
    uni.$u.http.setConfig((config) => {
        config.baseURL = 'https://api.shop.eduwork.cn'; 
        return config
    })
	uni.$u.http.interceptors.request.use((config) => {
	    config.data = config.data || {}
		config.header.Authorization =`Bearer ${vm.$store.state.indexStore.token}` 
	    return config 
	}, config => { 
	    return Promise.reject(config)
	})
	uni.$u.http.interceptors.response.use((response) => { 
		let message;
		const {statusCode,data} = response
		return data === undefined ? {} : data
	}, (response) => { 
		let message;
		const {statusCode,data} = response
			switch(statusCode){
				case 400:
					message = '服务器不理解请求的语法'
					break
				case 401:
					if(data.message === "Unauthorized"){
						message = '账号或者密码错误'
						break
					}
					message = '未登录或登录身份失效'
					break
				case 403:
					message = '无权限,服务器拒绝了请求'
					break
				case 422:
					const {errors:{email='',password='',avatar=''}} = data
					console.log(email,password,avatar)
					let errMsg = email ? email[0] : '' + password ? password[0] : ""  + avatar ? avatar[0] : ''
					uni.showToast({
						title:errMsg,
						icon:"none"
					})
			}
			message ? uni.showToast({
				title: message,
				icon:'none'
			}) : ''
			return false
	})
	uni.$u.patch = (url,params) => {
		const _params = {
			_method:'PATCH',
			...params
		}
		return uni.$u.http.post(url,_params)
	}
	uni.$u.api =  {
		...requsets
	}
}